package com.woniuxy.controller;

import com.woniuxy.pojo.User;
import com.woniuxy.service.UserService;
import com.woniuxy.util.ResultDto;
import org.apache.ibatis.annotations.Param;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;


@RestController
public class LoginController {
    @Autowired
    private UserService userService;
    @PostMapping(value = "/login.do")
    public ResultDto login(@Param("phone") String phone, @Param("lpass") String lpass){
        Subject subject=SecurityUtils.getSubject();
        UsernamePasswordToken token=new UsernamePasswordToken(phone,lpass);
        try {
            subject.login(token);
            User user=this.userService.findUserByTel(phone);
            //获取Session对象
            Session session=subject.getSession();
            session.setAttribute("USER_SESSION",user);
            return ResultDto.LOGIN_SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return ResultDto.LOGIN_FAILED;
        }
    }
}
